package com.virtualtrainer_01.viewListeners;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.text.format.Time;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.virtualtrainer_01.dao.ExerciseDB;
import com.virtualtrainer_01.dao.GroupDB;
import com.virtualtrainer_01.dao.TrainingDayDB;
import com.virtualtrainer_01.model.Exercise;
import com.virtualtrainer_01.model.Food;
import com.virtualtrainer_01.model.Group;
import com.virtualtrainer_01.model.Training;
import com.virtualtrainer_01.model.TrainingDay;
import com.virtualtrainer_01.util.NameList;

public class GenerateListAdapter extends BaseExpandableListAdapter {
	private Context context;
	private String[] parentList;
	private String[][] childList;

	public GenerateListAdapter(Context context, int day,
			NameList<TrainingDay> antr) {
		this.context = context;

		TrainingDayDB trainingDayDb = new TrainingDayDB(context);
		trainingDayDb.open();
		NameList<TrainingDay> antrenamente = antr;
		trainingDayDb.close();
		NameList<Group> groupList = new NameList<Group>();
		groupList = ((TrainingDay) antrenamente.get(day)).getTraining()
				.getGroupList();
		parentList = new String[groupList.size()];
		for (int i = 0; i < groupList.size(); i++)
			parentList[i] = groupList.get(i).getName();
		for (int i = 0; i < groupList.size(); i++)
			childList = new String[parentList.length][];
		NameList<Exercise> exList = ((TrainingDay) antrenamente.get(day))
				.getTraining().getExerciseList();
		for (int i = 0; i < parentList.length; i++) {
			ExerciseDB edb = new ExerciseDB(context);
			edb.open();
			NameList<Exercise> exGroupList = edb.getExerciseList(parentList[i]);
			edb.close();
			List<String> childListEx = new ArrayList<String>();
			for (int j = 0; j < exList.size(); j++)
				if (exGroupList.Contains((Exercise) exList.get(j)))
					childListEx.add(exList.get(j).getName());
			String[] childListExName = new String[childListEx.size()];
			for (int j = 0; j < childListEx.size(); j++)
				childListExName[j] = childListEx.get(j);
			childList[i] = childListExName;
		}
	}

	@Override
	public int getGroupCount() {
		return parentList.length;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return childList[groupPosition].length;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groupPosition;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return childList[groupPosition][childPosition];
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		TextView tv = new TextView(context);
		tv.setText(parentList[groupPosition]);
		tv.setPadding(40, 0, 0, 0);
		tv.setTextSize(40);
		return tv;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		TextView tv = new TextView(context);
		tv.setText(childList[groupPosition][childPosition]);
		tv.setPadding(60, 0, 0, 0);
		tv.setTextSize(25);
		return tv;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public String[][] getChildList() {
		return childList;
	}

	public void setChildList(String[][] childList) {
		this.childList = childList;
	}

}
